package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/v1/file/")
public class UploadController {
    @PostMapping("upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        System.out.println("fileName = " + fileName);
        String suffix =fileName.substring(fileName.lastIndexOf("."));
        System.out.println("suffix = " + suffix);
        fileName= UUID.randomUUID()+suffix;
        String dirPath="c:files";
        SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
        String dataPath=sdf.format(new Date());
        File dirFile=new File(dirPath+dataPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath=dirPath+dataPath+fileName;
        file.transferTo(new File(filePath));
        return JsonResult.ok(dataPath+fileName);
    }
}
